package com.cftpay.payexperience.usage.util;

import android.content.Context;
import android.os.Message;
import android.text.TextUtils;

import com.cftpay.payexperience.usage.constant.PayConstant;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.cftpay.payexperience.usage.constant.PayConstant.PRICEFAILURE;
import static com.cftpay.payexperience.usage.constant.PayConstant.PRICESUCCEED;

/**
 * Created by lj on 2017/6/10 0010.
 */

public class ChannelPrice {
    private String goodsPrice;

    public ChannelPrice() {
    }

    public void ChannelPrice(final Context context, String code, String type, final android.os.Handler handler) {
        String url = PayConstant.URL_JFT + PayConstant.PAYPEICE + "?p1_usercode=" + code + "&ext_paytypeforms=" + type;
        PayLogUtil.logBase("getChannelPrice" + "  url =  " + url);
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                PayLogUtil.logBase("getprice  failure");
                handler.sendEmptyMessage(PRICEFAILURE);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    goodsPrice = response.body().string();
                    PayLogUtil.logBase("getprice   successful");
                    if (!TextUtils.isEmpty(goodsPrice)) {
                        Message message = new Message();
                        message.what = PRICESUCCEED;
                        message.obj = goodsPrice;
                        handler.sendMessage(message);
                    } else {
                        handler.sendEmptyMessage(PRICEFAILURE);
                    }
                }
            }
        });
    }
}
